package com.cy.sports.service.mpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.sports.entity.Result;
import com.cy.sports.entity.TUser;
import com.cy.sports.mapper.TUserMapper;
import com.cy.sports.service.ITUserService;
import com.cy.sports.utils.MD5Utils;
import com.cy.sports.utils.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Garne
 * @since 2021-05-01
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService {
    @Resource
    TUserMapper userMapper;

    @Override
    public TUser login(String account, String password) {
        Map<String, Object> map = new HashMap();
        map.put("account", account);
        map.put("password", MD5Utils.encodeMemberPassword(password));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select().allEq(map);
        TUser tu = userMapper.selectOne(queryWrapper);
        if (null != tu) {
            tu.setLastLoginTime(LocalDateTime.now());
            userMapper.updateById(tu);
            tu.setPassword(null);
        }

        return tu;
    }

    @Override
    public Result<TUser> register(TUser user) {
        user.setCreateTime(LocalDateTime.now());
        user.setPassword(MD5Utils.encodeMemberPassword(user.getPassword()));
        user.setStatus(1);
        int r = userMapper.insert(user);
        if (r == 1) {
            return ResultUtil.define(0, "注册成功", null);
        }
        return ResultUtil.fail("注册失败，账号已注册");
    }

    @Override
    public Result delete(int optUserId, int targetId) {
        if (optUserId == targetId) {
            return ResultUtil.fail("非法操作，不能删除自己");
        }
        TUser user = userMapper.selectById(optUserId);
        if (null == user) {
            return ResultUtil.fail("参数错误");
        } else if (user.getRole() < 2) {
            return ResultUtil.fail("权限不足");
        }
        int r = userMapper.deleteById(targetId);
        if (r == 1) {
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }

    @Override
    public Result getUsers(int pageIndex, Integer status, Integer role) {
        IPage<TUser> page = new Page<>(pageIndex, 10);
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.select();
        Map map = new HashMap();
        if (null != status) {
            map.put("status", status);
        }
        if (null != role) {
            map.put("role", role);
        }
        wrapper.allEq(map);
        wrapper.orderByDesc("role","createTime");
        IPage<TUser> orders = userMapper.selectPage(page, wrapper);
        return ResultUtil.success(orders);
    }

    @Override
    public TUser selectById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    public Result updatePwd(TUser user) {
        if (null != user) {
            Map<String, Object> map = new HashMap();
            map.put("account", user.getAccount());
            map.put("password", MD5Utils.encodeMemberPassword(user.getPassword()));
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.select().allEq(map);
            TUser u = userMapper.selectOne(queryWrapper);
            if (null != u) {
                TUser tu = new TUser();
                tu.setId(user.getId());
                tu.setPassword(MD5Utils.encodeMemberPassword(user.getNewPassword()));
                if(userMapper.updateById(tu) == 1) {
                    return ResultUtil.success("修改成功");
                }
            }
        }
        return ResultUtil.fail("修改失败");
    }

    @Override
    public Result update(TUser user) {
        if (userMapper.updateById(user) == 1) {
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.fail("修改失败");
    }
}
